<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *音乐
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'one';
$tabs=array(
	array('op'=>'one','title'=>'单个上传'),
	array('op'=>'more','title'=>'批量上传'),	
);	
if($_RQ['op']=='one'){
	$item=array();
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>$tabs,
		'edit'=>array(
			array('name'=>'url','title'=>'音乐上传','type'=>'audio'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(!$postdata['url']){
		exi('请上传音乐!','error');
	}
	$info=$this->model('mp3')->getMP3Info($postdata['url']);
	$data=array(
		'title'=>trim($info['title']),
		'artist'=>trim($info['artist']),
		'album'=>trim($info['album']),
		'genre'=>trim($info['genre']),
		'url'=>trim($postdata['url']),
		'duration'=>trim($info['duration']),
		'year'=>trim($info['year']),
	);
	$id=pdo_getcolumn('jy_agentmcp_mp3',array('url'=>trim($postdata['url']),'uniacid'=>$_SESSION['uniacid']),'id');
	if($id){
		unset($data['url']);
		pdo_update('jy_agentmcp_mp3',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['createtime']=TIMESTAMP;
		pdo_insert('jy_agentmcp_mp3',$data);
	}
	exi('上传成功！','','one');
}
if($_RQ['op']=='more'){
	$item=array();
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'postzip',
		'tabs'=>$tabs,
		'edit'=>array(
			array ('name' => 'zip','type' => 'upfile','title' => '上传音乐压缩包','help'=>''),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='postzip'){
	$postdata=get_postdata();
	if(!$postdata['url']){
		exi('请上传音乐!','error');
	}
	$setting=core_setting('upload');
	$yes=str_array($setting[$type.'_ext']);
	foreach($yes as &$ye){
		$ye='.'.$ye;
	}
	$zip=cfc('files')->local($postdata['zip']);
	$temp=ROOT_AT.'temp/'.random(30);
	$zip=new ZipArchive();
	$zip->open($file);
	$zip->extractTo($temp);
	$files=cfc('files')->file_tree($temp,array(),$yes);
	foreach($files as $row){
		$index=strpos($row,'.');
		$ext=substr($filetitle,$index+1);
		$filetitle=substr($filetitle,0,$index);
		$url=cfc('files')->source_save($temp.$row,$type,$ext);
		$info=$this->model('mp3')->getMP3Info($url);
		$data=array(
			'title'=>trim($info['title']),
			'artist'=>trim($info['artist']),
			'album'=>trim($info['album']),
			'genre'=>trim($info['genre']),
			'duration'=>trim($info['duration']),
			'url'=>trim($url),
			'year'=>trim($info['year']),
		);
		$id=pdo_getcolumn('jy_agentmcp_mp3',array('url'=>trim($url),'uniacid'=>$_SESSION['uniacid']),'id');
		if($id){
			unset($data['url']);
			pdo_update('jy_agentmcp_mp3',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}else{
			$data['uniacid']=$_SESSION['uniacid'];
			$data['createtime']=TIMESTAMP;
			pdo_insert('jy_agentmcp_mp3',$data);
		}
	}
	exi('上传成功！','','more');
}